package com.netease.neliveplayer.playerkit.videoLoader;

import android.util.Log;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

abstract public class OnVideoListScrollEventHandler extends RecyclerView.OnScrollListener{
    private ArrayList<VideoLoader.VideoInfo>  videoList = new ArrayList<>();
    public void updateRoomList(ArrayList<VideoLoader.VideoInfo> list){
        if(list == null)
            return;
        videoList = list;
        preloadVideos();
    }
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
            int lastVisibleItem = layoutManager.findLastVisibleItemPosition();  // 最后一个可见 item
            preloadVideos(firstVisibleItem, lastVisibleItem);
            Log.d("onScrollStateChanged", "firstVisible:" + firstVisibleItem + "lastVisible:" +lastVisibleItem);
        }
    }
    private void preloadVideos() {
        VideoLoaderImpl.getInstance().preloadUrl(videoList);
    }
    // preload房间列表内指定位置区域的房间
    private void preloadVideos(int preloadFrom, int preloadTo) {
        VideoLoaderImpl.getInstance().preloadUrl(videoList, preloadFrom, preloadTo);
    }
}
